package online.lmds.sdmanage.main.message;

import online.lmds.sdmanage.common.utils.JsonUtils;
import online.lmds.sdmanage.main.device.dao.DeviceDao;
import online.lmds.sdmanage.main.device.dao.DeviceLogDao;
import online.lmds.sdmanage.main.device.dao.RunLogDao;
import online.lmds.sdmanage.main.device.entity.Device;
import online.lmds.sdmanage.main.device.entity.DeviceLog;
import online.lmds.sdmanage.main.device.entity.RunLog;
import online.lmds.sdmanage.main.message.dao.MqttLogDao;
import online.lmds.sdmanage.main.message.entity.MqttLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Brighten Ma on 2017/5/1.
 */

@Service("MqttHandlerService")
public class MqttHandlerService {

    @Resource(name = "MqttLogDao")
    private MqttLogDao mqttLogDao;

    @Resource(name = "DeviceLogDao")
    private DeviceLogDao deviceLogDao;

    @Resource(name = "DeviceDao")
    private DeviceDao deviceDao;

    @Resource(name = "RunLogDao")
    private RunLogDao runLogDao;

    public void handelMsg(String message) {
        MqttLog log = null;
        try{
            log = JsonUtils.stringToObject(message, MqttLog.class);
        } catch (Exception e){
            System.out.println("无效的消息类型");
            return;
        }

        if (log == null) {
            throw new RuntimeException("mqtt message fomat is irregular  !");
        }

        System.out.println(log.getMsgType());
        switch (log.getMsgType()) {
            case MqttLog.TYPE_NOTIFY_STATUS_DEVICE:
                handelNotifyDevcie(log);
                break;
            case MqttLog.TYPE_NOTIFY_STATUS_GROUP:
                // TODO: 2017/5/1
                break;
            case MqttLog.TYPE_NOTIFY_CTRL_DEVICE:
                handelNotifyCtrlDevice(log);
                break;
        }
    }


    /**
     * 设备状态更新消息处理
     * @param mqttLog
     */
    public void handelNotifyDevcie(MqttLog mqttLog) {
        Device device = MqttUtil.getDevice(mqttLog);

        deviceDao.updateStatusByUid(device);
        mqttLogDao.save(mqttLog);
        deviceLogDao.save(MqttUtil.getDeviceLog(mqttLog));
        handelRunLog(device);
    }


    /**
     * 设备控制信息更新消息处理
     * @param mqttLog
     */
    public void handelNotifyCtrlDevice(MqttLog mqttLog) {
        Device device = MqttUtil.getDevice(mqttLog);

        deviceDao.updateStatusByUid(device);

    // TODO: 2017/5/1
        if (mqttLog.getDeviceStatus() == Device.STATUS_ON
                || mqttLog.getDeviceStatus() == Device.STATUS_OFF) {
            deviceLogDao.save(MqttUtil.getDeviceLog(mqttLog));
            handelRunLog(device);

        }

    }


    /**
     * 处理设备运行记录
     * @param device    设备信息
     */
    public void handelRunLog(Device device){
        if (device == null || device.getUid() == null) {
            System.out.println("handelRunLog :device info is null");
            return;
        }

        int status = device.getStatus();

        RunLog runLogNew = new RunLog(device);
        RunLog runLogLast = runLogDao.getLast(runLogNew);

        if (status == Device.STATUS_ON){

            //开消息
            if (runLogLast != null && runLogLast.getStatus()  != null
            && status == runLogLast.getStatus()){
                runLogLast.setStatus(status);
                runLogDao.updateStatusByUid(runLogLast);
            } else {
                runLogDao.addNew(runLogNew);
            }

            System.out.println("handelRunLog :updateStatusByUid succeed!");
        } else {
            //关消息
            if ( runLogLast == null || runLogLast.getStatus() == null
            || runLogLast.getStatus() != Device.STATUS_ON){
                //最新记录为空或关闭，忽略
                return;
            }
            runLogLast.setStatus(status);
            runLogDao.updateStatusByUid(runLogLast);
            System.out.println("handelRunLog :updateStatusByUid succeed!");
        }


    }
}
